
#include "../Main.h"

#include "Button.h"
#include "Panel.h"
#include "TabPages.h"
#include "../TextRender.h"
#include "../TypedList.h"

static const u_int uTAB_PAGES_TITLE_BAR_HEIGHT = 22;
static const u_int uTAB_PAGES_TITLE_BTN_OFFSET_X = 4;
static const u_int uTAB_PAGES_TITLE_BTN_SPERATION = 2;
static const u_int uTAB_PAGES_TITLE_BTN_HEIGHT = 22;

class Tab
{
	public:

		Tab()
		{
			// The panel is managed by us
			m_pxPanel = new Panel( 0 );
		}
		~Tab()
		{
			delete m_pxPanel;
		}

		Button* m_pxButton;
		Panel* m_pxPanel;
};


TabPages::TabPages( Widget* pxParent ) : Panel( pxParent )
{
	m_pxTabs = new TypedList< Tab >( true );
	m_iActiveTab = INVALID_HANDLE;

	m_pxTabHeaderPanel = new Panel( this );
	m_pxTabHeaderPanel->SetAnchors( ANCHOR_TOP | ANCHOR_LEFT | ANCHOR_RIGHT );
	m_pxTabHeaderPanel->SetPosition( pxParent->GetResizedX(), pxParent->GetResizedY() );
	m_pxTabHeaderPanel->SetSize( pxParent->GetResizedWidth(), uTAB_PAGES_TITLE_BAR_HEIGHT );
}

TabPages::~TabPages()
{
	delete m_pxTabs;
}

FHandle TabPages::AddTab( const char* szTitle )
{
	Tab* pxNewTab = new Tab();
	
	u_int uBtn_X_Pos = m_pxTabHeaderPanel->GetResizedX() + uTAB_PAGES_TITLE_BTN_OFFSET_X;
	if( m_pxTabs->Count() > 0 )
	{
		 Tab* pxLastTab = m_pxTabs->Last();
		 uBtn_X_Pos = pxLastTab->m_pxButton->GetResizedX() + pxLastTab->m_pxButton->GetResizedWidth() + uTAB_PAGES_TITLE_BTN_SPERATION;
	}

	// Add tab panel

	pxNewTab->m_pxPanel->SetHasBorder( true );
	pxNewTab->m_pxPanel->SetPosition( GetResizedX(), GetResizedY() + uTAB_PAGES_TITLE_BAR_HEIGHT );
	pxNewTab->m_pxPanel->SetSize( GetResizedWidth(), GetResizedHeight() - uTAB_PAGES_TITLE_BAR_HEIGHT );
	pxNewTab->m_pxPanel->SetAnchors( ANCHOR_LEFT | ANCHOR_RIGHT | ANCHOR_TOP | ANCHOR_BOTTOM );
	pxNewTab->m_pxPanel->SetVisible( false );

	// Add title button

	Button* pxTitleBtn = new Button( m_pxTabHeaderPanel );

	pxTitleBtn->SetPosition( uBtn_X_Pos, m_pxTabHeaderPanel->GetResizedY() + ( uTAB_PAGES_TITLE_BAR_HEIGHT - uTAB_PAGES_TITLE_BTN_HEIGHT ) );
	
	TextRender::SetFont( FONT_SANS_12 );
	u_int uBtnTxtWid, uBtnTxtHi;
	TextRender::GetRenderSize( szTitle, uBtnTxtWid, uBtnTxtHi );
	
	pxTitleBtn->SetSize( uBtnTxtWid + 16, uTAB_PAGES_TITLE_BTN_HEIGHT );
	pxTitleBtn->SetLabel( szTitle );
	pxTitleBtn->SetButtonPressCallback( TabClickCallback, pxTitleBtn );
	pxTitleBtn->SetFlatStyle( true );
	pxTitleBtn->SetBorderWidth( 1.0f );

	pxNewTab->m_pxButton = pxTitleBtn;

	// Add to the tab list

	m_pxTabs->Add( pxNewTab );

	FHandle iNewHandle = m_pxTabs->Count() - 1;

	if( m_iActiveTab == INVALID_HANDLE )
	{
		SetActiveTab( iNewHandle );
	}

	return static_cast< FHandle >( iNewHandle );
}

void TabPages::DeleteTab( int iIndex )
{
	// Assert instead?
	if( iIndex > (int)m_pxTabs->Count() - 1 )
	{
		return;
	}
	
	m_pxTabs->Delete( iIndex );
}

Panel* TabPages::GetTabPanel( FHandle iHandle )
{
	if( iHandle >= 0 && iHandle < (int)m_pxTabs->Count() )
	{
		Tab* pxTab = m_pxTabs->Get( iHandle );
		return pxTab->m_pxPanel;
	}
	return 0;
}

void TabPages::SetActiveTab( FHandle iHandle )
{
	if( iHandle >= 0 && iHandle < (int)m_pxTabs->Count() )
	{
		if( m_iActiveTab != INVALID_HANDLE )
		{
			Tab* pxTab = m_pxTabs->Get( m_iActiveTab );
			pxTab->m_pxPanel->SetVisible( false );
			pxTab->m_pxButton->SetFlatStyle( true );
			pxTab->m_pxButton->SetEnabled( true );
		}

		m_iActiveTab = iHandle;

		Tab* pxTab = m_pxTabs->Get( m_iActiveTab );
		pxTab->m_pxPanel->SetVisible( true );
		pxTab->m_pxButton->SetFlatStyle( false );
		pxTab->m_pxButton->SetEnabled( false );		
	}
}

void TabPages::Update()
{
	Widget::Update();

	// Just update the active tab
	FAssert( ( m_pxTabs != 0 ) && ( m_iActiveTab >= 0 ) && ( m_iActiveTab < (int)m_pxTabs->Count() ) );
	
	Tab* pxActiveTab = m_pxTabs->Get( m_iActiveTab );
	FAssert( pxActiveTab != 0 && pxActiveTab->m_pxPanel != 0 );
	pxActiveTab->m_pxPanel->Update();
}

void TabPages::Render()
{
	Widget::Render();

	// Just render the active tab
	FAssert( ( m_pxTabs != 0 ) && ( m_iActiveTab >= 0 ) && ( m_iActiveTab < (int)m_pxTabs->Count() ) );
	
	Tab* pxActiveTab = m_pxTabs->Get( m_iActiveTab );
	FAssert( pxActiveTab != 0 && pxActiveTab->m_pxPanel != 0 );
	pxActiveTab->m_pxPanel->Render();

}

void TabPages::TabClickCallback( void* pData )
{
	// Bit ugly, climb the widget heirarchy to find the tab control and which button was pressed

	Button* pxBtn = reinterpret_cast< Button* >( pData );
	Widget* pxBtnPanel = pxBtn->GetParent();

	FHandle iTab = pxBtnPanel->GetChildren()->IndexOf( pxBtn );

	TabPages* pxTabs = reinterpret_cast< TabPages* >( pxBtnPanel->GetParent() );
	pxTabs->SetActiveTab( iTab );
}

void TabPages::SetColour( const u_int uColour )
{
	m_pxTabHeaderPanel->SetColour( uColour );
	Panel::SetColour( uColour );
}

int TabPages::GetNumTabs()
{
	return m_pxTabs->Count();
}

// eof
